from django import test
from django import template
from models import Menu,MenuItem
from templatetags.nav_menu import navmenu


class TestNavMenu(test.TestCase):
    
    def setUp(self):
        self.menu = Menu.objects.create(name='teste',slug='teste')
        item = MenuItem.objects.create(parent_menu=self.menu,name='item 2',link='/',order=2)
        item = MenuItem.objects.create(parent_menu=self.menu,name='item 1',link='/',order=1)
    
    def testCreation(self):
        self.assertTrue(2,self.menu.items.count())
    
    
    def testOrdering(self):
        self.assertTrue(2,self.menu.items.all()[0].order)
        self.assertTrue(1,self.menu.items.all()[1].order)
        

def plain(string):
    return string.replace('\n','').replace('  ','').replace('\t',' ')

class TestTemplateTag(test.TestCase):
    
    def setUp(self):
        pass
    
    def testEmptyNavmenu(self):
        t = template.Template('{% load nav_menu %}{% navmenu "ul" %}')
        self.assertEqual('',plain(t.render(template.Context({}))))
        t = template.Template('{% load nav_menu %}{% navmenu "ul" %}')
        self.assertEqual('',plain(t.render(template.Context({}))))
        
    def testNavmenu(self):
        self.menu = Menu.objects.create(name='Teste',slug='teste')
        item = MenuItem.objects.create(parent_menu=self.menu,name='item 2',link='/',order=2)
        item = MenuItem.objects.create(parent_menu=self.menu,name='item 1',link='/',order=1,always_visible=True)

        t = template.Template('{% load nav_menu %}{% navmenu "teste" %}')
        self.assertEqual('<li class="always_visible"><a href="/">item 1</a></li><li class=""><a href="/">item 2</a></li>',plain(t.render(template.Context({}))))
